feat(dataframe): add writeCsv with CsvWriteOptions#53
Open
LantaoJin wants to merge 1 commit into
Open
Conversation
Closes apache#38 Mirrors writeParquet's surface for CSV: writeCsv(path) and writeCsv(path, options). Options class exposes singleFileOutput, partitionCols, hasHeader, delimiter, quote, escape, nullValue, and fileCompressionType. Uses the proto-over-JNI pattern (CsvWriteOptionsProto) rather than writeParquet's wide-JNI signature because the writer-side option set is much wider; a single byte[] keeps the JNI signature stable as more fields are added later. FileCompressionType is reused from csv_read_options.proto since the codec set is identical between read and write. Option<CsvOptions> on the Rust side stays None when no writer-side knob is set, so DataFusion's runtime defaults are preserved. Tests: 4 new options round-trip tests plus 7 integration tests (default round-trip, single-file, custom delimiter, gzip round-trip, retain-after-write, null path/options rejection). make test passes; cargo clippy/fmt and spotless are clean.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
DataFrame.writeParquet(#27) lets Java callers materialize a query result, but CSV remains read-only. DataFusion supportsDataFrame::write_csvupstream with the full writer-side option surface (delimiter, quote, escape, null token, compression, partitioning, single-file vs directory output). Issue #38 tracks exposing it on the Java side.The CSV write surface is wider than parquet's — six writer-side knobs plus
singleFileOutputandpartitionCols— so this PR uses the proto-over-JNI pattern (introduced in #29 and reused by all the read-side option classes) instead of the wide-JNI patternwriteParquetshipped with. Sending a singlebyte[]keeps the JNI signature stable as more knobs are added.What changes are included in this PR?
proto/csv_write_options.proto— newCsvWriteOptionsProtomessage. Fields areoptionalso unset values preserve DataFusion's defaults;partition_colsisrepeatedso the empty list round-trips unambiguously.FileCompressionTypeis reused fromcsv_read_options.protobecause the codec set is identical between read and write at the upstream level. Promoting the enum to a sharedcompression.protoin PR feat(json): expose NdJsonReadOptions via registerJson and readJson #47. Once that lands, this PR's import switches one line.CsvWriteOptionsJava builder mirroring the upstreamCsvOptionswriter-side API:singleFileOutput,partitionCols,hasHeader,delimiter,quote,escape,nullValue,fileCompressionType. All defaults are unset (null) so callers only pay for knobs they touch.DataFrame.writeCsv(String)andDataFrame.writeCsv(String, CsvWriteOptions)overloads with up-front null-arg validation. The receiver remains usable after the call, matchingwriteParquet's "retain after write" semantics.Java_org_apache_datafusion_DataFrame_writeCsvWithOptionsJNI handler innative/src/csv.rs(co-located with the read-side handlers since they share the proto-decode plumbing). Decodes the proto, buildsDataFrameWriteOptionsand anOption<CsvOptions>, then callsDataFrame::write_csv.Option<CsvOptions>is left asNonewhen no writer knob is set so DataFusion's defaults apply.Out of scope (for follow-ups):
CsvOptionsbut not in feat: add DataFrame.writeCsv with CsvWriteOptions #38's checklist:terminator,doubleQuote,dateFormat,datetimeFormat,timestampFormat,timestampTzFormat,timeFormat,compressionLevel,truncatedRows. Easy follow-up — same proto, just add fields.compression_level— separate from the compression codec; the upstreamCsvOptionsexposeswith_compression_levelbut the issue doesn't list it.Are these changes tested?
Yes, 11 new tests across
CsvWriteOptionsTestandDataFrameWriteCsvTest.Are there any user-facing changes?
Yes, purely additive. New public API:
org.apache.datafusion.CsvWriteOptionsDataFrame.writeCsv(String)DataFrame.writeCsv(String, CsvWriteOptions)The new
org.apache.datafusion.protobuf.CsvWriteOptionsProtogenerated class is also exposed via the protobuf-Java output, consistent with how the read-side option protos are exposed. No API removals, no deprecations, no behavior change for existing callers.